var api = require('../../api.js');
var app = getApp();
// pages/personal/allow/allow.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getUserInfo(arg) {
    console.log("获取到的用户信息")
    console.log(arg)
    wx.login({
      success: function (res) {
        if (res.code) {
          console.log("获取到的code")
          console.log(res.code)
          getApp().loginrequest({
                url: api.passport.login,
                method: "post",
                data: {
                  code: res.code,
                  userInfo: arg.detail.rawData,
                  encrypted_data: arg.detail.encryptedData,
                  iv: arg.detail.iv,
                  signature: arg.detail.signature
                },
                success: function (res) {
                  console.log("真正的登陆成功userlogin")
                  console.log(res)
                  wx.hideLoading();
                  if (res.code == 200) {
                    const tokenStr = res.data.tokenHead + res.data.token
                    wx.setStorageSync('loginToken', tokenStr)
                    wx.setStorageSync('token', res.data.token);
                    wx.setStorageSync("access_token", res.data.userId);
                    wx.setStorageSync("user_info", {
                      avatar_url: res.data.img,
                      nickname: res.data.username,
                      avatar_url: res.data.icon,
                      // parent: res.data.parent,
                      id: res.data.userId
                    });
                    wx.setStorageSync("my_info", {
                      supplyId: res.data.myInfo.supplyId,
                      supplyState: res.data.myInfo.supplyState,
                      myMoney: res.data.myInfo.myMoney,
                      icon: res.data.myInfo.icon,
                      nickname: res.data.myInfo.nickname,
                      memberLevelId: res.data.myInfo.memberLevelId,
                      id: res.data.myInfo.id,
                      city: res.data.myInfo.city,
                      gender: res.data.myInfo.gender,
                      job: res.data.myInfo.job,
                      phone: res.data.myInfo.phone,
                      birthday: res.data.myInfo.birthday,



                      // parent: res.data.parent,
                    });

                    var parent_id = wx.getStorageSync("parent_id");
                    var p = getCurrentPages();
                    // var parent_id = 0;


                    if (p[0].options.user_id != undefined) {

                      parent_id = p[0].options.user_id;
                    }
                    else if (p[0].options.scene != undefined) {
                      parent_id = p[0].options.scene;
                    }
                    console.log('parentid:' + parent_id, p[0].options.scene, p[0].options.user_id);
                  

                    if (page == undefined) {
                      return;

                    }
                    wx.redirectTo({
                      url: "/" + page.route + "?" + util.objectToUrlParams(page.options),
                      fail: function () {
                        wx.switchTab({
                          url: "/" + page.route,
                        });
                      },
                    });
                  } else {
                    // wx.showToast({
                    //   title: res.msg
                    // });
                  }
                }
              });
            }
        

      }
    });
    if ( arg.detail.iv ) {
      console.log("6666666")
      console.log(arg.detail)
      if (wx.reLaunch) {
        wx.reLaunch({
          url: '/pages/index/index'
        })
      } else {
        this.location('/pages/index/index');
      }
   //   app.locationReplace('/pages/index/index');
    }
  }
})